import {getEcCom} from '../../util/convert';
import {trim} from '../../util/public/common';

export default class Main extends React.Component {
    constructor(props) {
        super(props);
        if (!window.FORM_CK_REF) {
            window.FORM_CK_REF = {};
        }
    }

    render() {
        const {WeaRichText} = getEcCom();
        const {editorId, ls = false, extentsConfig = [], value, textheight = 4,placeholder=""} = this.props;
        const ckConfig = {};
        ckConfig.toolbar = [['Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', '-',
            'Format', 'Font', 'FontSize','lineheight', 'TextColor', 'BGColor', '-',
            'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
            'Link', 'Unlink', '-', 'Table', '-', 'Iframe', 'HorizontalRule', 'SpecialChar', '-', 'CopyFormatting', 'RemoveFormat', 'PasteText',
            'Undo', 'Redo', 'Maximize','Image'
        ]];

//		 	            { name: 'document', items: [ 'Source'] },
//		 	            { name: 'basicstyles', items: [ 'Bold', 'Italic', 'Underline', 'Strike', ] },
//		 	            { name: 'styles', items: [ 'Format','Font', 'FontSize' ] },
//		 	            { name: 'colors', items: [ 'TextColor','BGColor' ] },
//		 	            { name: 'paragraph', items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent','-','JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'] },
//		 	            { name: 'links', items: [ 'Link', 'Unlink' ] },
//						{ name: 'tools', items: [ 'Maximize' ] },
//		 	            { name: 'insert', items: [ 'Table', '-','Iframe','HorizontalRule','SpecialChar','-','CopyFormatting', 'RemoveFormat','PasteText'] },
//		 	            { name: 'clipboard' ,items:['Undo', 'Redo']}

//		ckConfig.toolbar = [
//			            { name: 'all', items: [ 'Source','-',
//												'Bold', 'Italic', 'Underline', 'Strike','-',
//												'Format','Font', 'FontSize','-',
//												'TextColor','BGColor' ,'-',
//												'NumberedList', 'BulletedList', '-',
//												'Outdent', 'Indent','-',
//												'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock','-',
//												'Link', 'Unlink' ,'-',
//												'Image', 'Table', '-',
//												'Iframe','HorizontalRule','SpecialChar','-',
//												'CopyFormatting', 'RemoveFormat','PasteText','-',
//												'Undo', 'Redo']},
//			        ];

        // ckConfig.extraPlugins = 'autoautogrow';
        ckConfig.height = 40 * textheight;
        // ckConfig['autoGrow_maxHeight'] = 40 * textheight;
        ckConfig.removePlugins = 'resize';
        ckConfig.uploadUrl = '/api/doc/upload/uploadFile';
        ckConfig.fontSize_defaultLabel = '12';
        // ckConfig.font_names = '仿宋_GB2312/FangSong_GB2312;宋体/SimSun;微软雅黑/Microsoft YaHei;楷体/KaiTi;黑体/SimHei;Arial/Arial, Helvetica, sans-serif;新宋体/NSimSun;楷体_GB2312/KaiTi_GB2312;';
        ckConfig.font_defaultLabel = '微软雅黑';
        // ckConfig.fontSize_sizes = '8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px';
        ckConfig.startupFocus = false;
        ckConfig.placeholder = placeholder;
        const ckprops = {
            id: editorId,
            ref: editorId,
            ls: ls,
            ckConfig: ckConfig,
            value: value,
            scrollContainer: '.wea-new-top-req-content',
            placeholder:placeholder
        };
        return (
            <WeaRichText ecId={`${this && this.props && this.props.ecId || ''}_WeaRichText@7gavv2`} {...ckprops}
                         onBlur={this.doOnChangeEvent}
                         onToolsChange={this.buttonBtnChange}
                         onStatusChange={this.doStatusChange}
            />
        )
    }


    doOnChangeEvent = (value) => {
        this.props.onChange && this.props.onChange(value);
    }

    buttonBtnChange = (name = '', ids = '', list = [], type = '') => {
        const {editorId} = this.props;
        let html = [];
        if (name === 'Upload') {
            if (type === 'image') {
                list.map(item => {
                    const imglink = `/weaver/weaver.file.FileDownload?fileid=${item.fileidCode}`;
                    html.push(`<img src='${imglink}' />&#8203;`);
                });
            }
        }
        return html.join('');
    }

    doStatusChange = (status) => {
        if (status === "ready") {
            const {editorId} = this.props;
            window.FORM_CK_REF[editorId] = this.refs[editorId];
        }
    }

    ckfocus() {
        const {editorId} = this.props;
        this.refs[editorId].getEditor().focus();
    }
}
